package com.yuri.news.controller;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuri.news.domain.Result;
import com.yuri.news.entity.Depa;
import com.yuri.news.service.DepaService;

/**
 * 部门控制器
 * 
 * @author Yuri
 *
 */

// 允许跨域访问
@CrossOrigin
// 控制器
@Controller
// 以json形式返回
@ResponseBody
public class DepaController {
	// 日志打印
	private final Logger logger = LoggerFactory.getLogger(DepaController.class);

	@Autowired
	DepaService depaService;

	/**
	 * 添加部门
	 * 
	 * @param depa
	 * @return
	 */
	@RequestMapping(value = "addDepa", method = RequestMethod.POST)
	public Result<Depa> addDepa(Depa depa) {
		logger.info("\n 添加部门（addDepa）开始：depa = " + depa);
		Result<Depa> result = new Result<Depa>();
		try {
			if(depa.getUpDepa() != null && -1 == depa.getUpDepa().getId()) {
				depa.setUpDepa(null);
			}
			Depa dpa = depaService.saveDepa(depa);
			if(dpa != null) {
				result.setSuccess("添加成功！");
			}else {
				result.setError("添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 添加部门（addDepa）结束：result = " + result);
		return result;
	}

	/**
	 * 根据id删除部门
	 * 
	 * @param id
	 */
	@RequestMapping(value = "delDepa", method = RequestMethod.DELETE)
	public Result<Depa> delDepa(Long id) {
		// 日志打印
		logger.info("\n 删除部门(delDepa)开始 Depa.id = " + id);
		// 设置返回数据
		Result<Depa> result = new Result<>();
		try {
			if(depaService.deleteDepa(id)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("删除失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 删除部门(delDepa)结束 result = " + result);
		return result;
	}

	/**
	 * 修改部门信息
	 * 
	 * @param depa
	 */
	@RequestMapping(value = "modifyDepa", method = RequestMethod.PUT)
	public Result<Depa> modifyDepa(Depa depa) {
		// 日志打印
		logger.info("\n 修改部门(modifyDepa)开始 Depa = " + depa);
		Result<Depa> result = new Result<>();
		try {
			if(depa.getUpDepa() != null && depa.getUpDepa().getId() == -1) {
				depa.setUpDepa(null);
			}
			if(depaService.updateDepa(depa)) {
				result.setSuccess("更新成功!");
			}else {
				result.setError("更新失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 修改部门(modifyDepa)结束 result = " + result);
		return result;
	}

	/**
	 * 部查询所有部门
	 * 
	 * @param depa
	 */
	@RequestMapping(value = "findDepaAll", method = RequestMethod.GET)
	public Result<Depa> findDepaAll() {
		// 日志打印
		logger.info("\n 查询所有部门（findDepaAll）");
		Result<Depa> result = new Result<>();
		try {
			List<Depa> depas = depaService.listDepa();
			
			if(depas != null && depas.size() > 0) {
				result.setData(depas);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂时无数据！请稍后再试");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		// 返回数据
		logger.info("\n 查询所有部门(findDepaAll)结束  result = " + result);
		return result;
	}

	/**
	 * 根据id查询部门
	 * 
	 * @param depa
	 */
	@RequestMapping(value = "findDepaById", method = RequestMethod.GET)
	public Result<Depa> findDepaAll(Long id) {
		// 日志打印
		logger.info("\n 根据id查询部门(findDepaById)开始 id = " + id);
		List<Depa> depas = new ArrayList<>();
		Result<Depa> result = new Result<>();
		
		try {
			Depa depa = depaService.getDepa(id);
			if(depa != null) {
				depas.add(depa);
				result.setData(depas);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂时无数据！请稍后再试");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id查询部门(findDepaById)结束 result = " + result);
		return result;
	}

	/**
	 * 根据Key关键字查询部门
	 * @param key		关键字
	 * @param page		当前页
	 * @param size		每页数据大小
	 * @return
	 */
	@RequestMapping(value = "findDepaByKey", method = RequestMethod.GET)
	public Result<Depa> findDepaByKey(@RequestParam(name = "key",defaultValue = "") String key ) {
		// 日志打印
		logger.info("\n 根据Key关键字查询部门(findDepaByKey)开始 key = " + key);
		Result<Depa> result = new Result<>();
		try {
			List<Depa> depas = depaService.listDepaByKey(key);
			
			if(depas != null && depas.size() > 0) {
				result.setData(depas);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂时无此用户信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据Key关键字查询部门(findDepaByKey)结束 result = " + result);
		return result;
	}
}
